const cutFile = (file) => {
  // const chunkSize = 1024 * 1024 * 5; // 5MB
  // const cutCount = Math.ceil(file.size / chunkSize);

  // let chunks = [];
  // for (let i = 0; i < cutCount; i++) {
  //   const chunk = await createChunk(file, i, chunkSize);
  //   chunks.push(chunk);
  // }
  // return chunks;
  return new Promise((resolve, reject) => {
    const chunkSize = 1024 * 1024 * 5; // 5MB
    const thread_count = navigator.hardwareConcurrency || 4;
    const result = [];
    let finishCount = 0;
    const cutCount = Math.ceil(file.size / chunkSize);
    const workerChunkCount = Math.ceil(cutCount / thread_count);
    for (let i = 0; i < thread_count; i++) {
      const worker = new Worker(new URL('../worker.js', import.meta.url), {
        type: 'module',
      });
      // 计算开始索引和结束索引
      const startIndex = i * workerChunkCount;
      const endIndex = Math.min(startIndex + workerChunkCount, cutCount);
      // 发送任务给worker
      worker.postMessage({
        file,
        chunkSize,
        startIndex,
        endIndex,
      });
      worker.onmessage = (e) => {
        for (let i = startIndex; i < endIndex; i++) {
          result[i] = e.data[i - startIndex];
        }
        worker.terminate();
        finishCount++;
        if (finishCount === thread_count) {
          resolve(result);
        }
      };
    }
  });
};
export default cutFile;
